from django.db import models
from django.db.models import Q, F

# Create your models here.
'''
    DepartmentManager:部门表管理器
    create：创建科室
    update：修改科室
    remove：删除科室
    query ：查询科室
'''
class DepartmentManager(models.Manager):
    def create(self, deptName):
        departments = self.model()
        departments.deptName =deptName
        departments.save()
        return departments
    def update(self, id, deptName):
        print('a')
        self.filter(pk = int(id)).update(deptName=deptName)
        departments = self.filter(pk = int(id)).values()
        return departments
    def remove(self, id):
        self.filter(pk = int(id)).delete()
        departments = self.filter(pk = int(id)).values()
        return departments
    def query(self, id):
        departments = self.filter(deptName__contains = id).values()
        return departments
'''
    Department:部门表
        deptName:科室
        departments:管理器实例化
'''
class Department(models.Model):
    deptName = models.CharField(max_length=20)
    departments = DepartmentManager()
    class Meta():
        db_table = 'Department'

'''
    PositionManager:职务表管理器  
    create：创建职务
    update：修改职务
    remove：删除职务
    query ：查询职务     
'''
class PositionManager(models.Manager):
    def create(self, posName):
        positions = self.model()
        positions.posName = posName
        positions.save()
        return positions
    def update(self, id, posname):
        self.filter(pk = int(id)).update(posName=posname)
        positions = self.filter(pk = int(id)).values()
        return positions
    def remove(self, id):
        self.filter(pk = int(id)).delete()
        positions = self.filter(pk = int(id)).values()
        return positions
    def query(self, id):
        positions = self.filter(posName__contains = id).values()
        print(positions)
        return positions
'''
    Position：职务表
        posName：职务名
        positions：管理器实例化
'''
class Position(models.Model):
    posName = models.CharField(max_length=20)
    positions = PositionManager()

    class Meta():
        db_table = 'Position'

'''
    StaffManager：员工表管理器
        create：创建员工
        update：修改员工
        remove：删除员工
        query ：查询员工
        approveQuery：查询员工的上级主管
'''
class StaffManager(models.Manager):
    def create(self, job, name, gender, departments, positions, isDelete=False):
        staffs = self.model()
        staffs.job = job
        staffs.name = name
        staffs.gender = gender
        staffs.departments_id = departments
        staffs.positions_id = positions
        staffs.isDelete = isDelete
        staffs.save()
        return staffs
    def remove(self, id):
        self.filter(pk = int(id)).update(isDelete = True)
        staffs = self.filter(pk = int(id)).values()
        return staffs
    def update(self, id, job, name, gender, departments, positions, isDelete=False):
        self.filter(pk = int(id)).update(job = job)
        self.filter(pk = int(id)).update(name = name)
        self.filter(pk = int(id)).update(gender = gender)
        self.filter(pk = int(id)).update(departments = departments)
        self.filter(pk = int(id)).update(positions = positions)
        self.filter(pk = int(id)).update(isDelete = isDelete)
        staffs = self.filter(pk = int(id)).values()
        return staffs
    def query(self, id):
        staffs = self.filter(isDelete = False).filter(Q(job__contains = id)|Q(name__contains = id)|Q(gender__contains = id)).values()
        return staffs
    def approveQuery(self, id):
        staff = self.filter(pk = id)[0]
        staffs = self.filter(positions = staff.positions_id+1).filter(departments=staff.departments_id)
        return staffs
'''
    Staff：员工表
        job：职称
        name：姓名
        gender：性别
        departments：部门id
        positions：职务id 
        isDelete：是否删除
        staffs ：管理器实例化
'''
class Staff(models.Model):
    job = models.CharField(max_length = 20)
    name = models.CharField(max_length = 20)
    gender = models.CharField(max_length = 2)
    departments = models.ForeignKey(Department, on_delete=models.CASCADE)
    positions = models.ForeignKey(Position, on_delete=models.CASCADE)
    isDelete = models.BooleanField(default = False)
    staffs = StaffManager()

    class Meta():
        db_table = 'Staff'

